package com.dx.mes.service.webservice.service;

import com.dx.common.table.his.His_ret_prd_info;
import com.dx.common.table.ppt.Bis_data;
import com.dx.common.table.ppt.Ret_line_wo;
import com.dx.common.table.ppt.Ret_wo;
import com.dx.common.table.ppt.Trans_prd_sap;
import com.dx.mes.common.bean.erp.FPQCRPT;
import com.dx.mes.common.core.rabbitmq.BaseConnFactory;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.transcoder.util.GUIDGenerator16;
import com.dx.mes.common.transcoder.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.EVT_MES_SAP_PQC;

/**
 * Created by admin on 2018/4/16.
 */
@Service("msappqcService")
public class MsappqcService {

    @Autowired
    private IHisRetPrdInfoDao hisRetPrdInfoDao;
    @Autowired
    private ITransPrdSapDao transPrdSapDao;
    @Autowired
    private IRetLineWoDao retLineWoDao;
    @Autowired
    private IBisDataDao bisDataDao;
    @Autowired
    private IRetWODao retWODao;
    @Autowired
    private IBisOpeDao bisOpeDao;
    @Resource
    private BaseConnFactory baseConnFactory;

    public void subMainProc() {

        //获得所有判等站点
        String sql ="select data_ext from  bis_data  where data_cate='DWPR' and ext_3='Y'";

        List<String> sapWork= bisDataDao.findBySQL(sql);

        List<His_ret_prd_info> his_ret_prd_infoAll = new  ArrayList<>();//所有的报工屏体
        for(String ope:sapWork){
            //获得这个报工站点的上一次报工时间
            String sqlM = "select * from trans_prd_sap where ope_id_fk = '" +ope+"' and evt_cate='MSPQ'";
            List<Trans_prd_sap> sapList = transPrdSapDao.findBySQL(sqlM,Trans_prd_sap.class);

            if(sapList!=null && sapList.size()>0){
                Trans_prd_sap trans_prd_sap = sapList.get(0);
                Format dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date  = dateFormat.format(trans_prd_sap.getMes_update_time());
                String hql ="FROM His_ret_prd_info where cr_ope_id_fk='" +ope+"' and evt_cate='LGOF'" +
                        " and evt_timestamp > to_date('" +date+"','yyyy-mm-dd hh24:mi:ss') and ext_9='NG'";
                List<His_ret_prd_info> his_ret_prd_infos = hisRetPrdInfoDao.find(hql);
                his_ret_prd_infoAll.addAll(his_ret_prd_infos);
            }else {//该站点第一次报工
                String hql ="FROM His_ret_prd_info where cr_ope_id_fk='" +ope+"' and evt_cate='LGOF' and ext_9='NG'";
                List<His_ret_prd_info> his_ret_prd_infos = hisRetPrdInfoDao.find(hql);
                his_ret_prd_infoAll.addAll(his_ret_prd_infos);
            }
        }

        Format dateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = dateFormat.format(new Date());
        Format dateFormat1 = new SimpleDateFormat("HHmmss");
        String time = dateFormat1.format(new Date());

        List<FPQCRPT> fpqcrptList = new ArrayList<>();
        for(His_ret_prd_info his_ret_prd_info :  his_ret_prd_infoAll){

            String hqlA ="from Bis_data where data_cate='DFCT' and data_id='"+his_ret_prd_info.getDef_code()+"'";
            List<Bis_data>  bisData = bisDataDao.find(hqlA);
            //工单，线别
            Ret_wo ret_wo = retWODao.get(his_ret_prd_info.getWo_id_fk());
            String hqlX ="from Ret_line_wo where wo_id_fk='" +ret_wo.getWo_id()+"' and  ope_id_fk='J0800' ";
            List<Ret_line_wo>  lineWos = retLineWoDao.find(hqlX);

            String sqlN ="select * from bis_data where data_cate='DWPR' and ext_2='" + ret_wo.getMdl_id_fk() + "' and data_ext='" +his_ret_prd_info.getCr_ope_id_fk()+"'";
            List<Bis_data> opeList = bisDataDao.findBySQL(sqlN,Bis_data.class);

            FPQCRPT fpqcrpt = new FPQCRPT();
            fpqcrpt.setWERKS(ret_wo.getFab_id_fk());
            fpqcrpt.setAUFNR(ret_wo.getWo_id());
            if (opeList!=null && opeList.size()>0){
                fpqcrpt.setVORNR(opeList.get(0).getExt_5());
            }
            fpqcrpt.setZXT(lineWos.get(0).getLine_id_fk());
            fpqcrpt.setZZD(his_ret_prd_info.getCr_ope_id_fk());
            fpqcrpt.setMATNR(ret_wo.getMdl_id_fk());
            fpqcrpt.setZMESLH(his_ret_prd_info.getPrd_seq_id());
            fpqcrpt.setZJYJG("NG");
            fpqcrpt.setZQXDM(his_ret_prd_info.getDef_code());
            fpqcrpt.setZQXYY(bisData.get(0).getData_desc());
            fpqcrpt.setZBZ(his_ret_prd_info.getSht_ope_msg());
            fpqcrpt.setZPDDJ(his_ret_prd_info.getPrd_grade());
            fpqcrpt.setZJYRQ(date);
            fpqcrpt.setZJYSJ(time);
            fpqcrptList.add(fpqcrpt);
        }

        Timestamp current_timestamp = Util.getCurrentTimestamp();
        List<Trans_prd_sap>  newTranList = new ArrayList<>();
        for(String ope:sapWork){
            Trans_prd_sap trans_prd_sap = null;
            String sqlY = "select * from trans_prd_sap where ope_id_fk = '" +ope+"' and evt_cate='MSPQ'";
            List<Trans_prd_sap> opelist = transPrdSapDao.findBySQL(sqlY,Trans_prd_sap.class);
            if(opelist!=null && opelist.size()>0){
                trans_prd_sap = opelist.get(0);
                trans_prd_sap.setOpe_id_fk(ope);
                trans_prd_sap.setMes_update_time(current_timestamp);
                trans_prd_sap.setPrd_qty(0);
                trans_prd_sap.setSap_id("");
                trans_prd_sap.setEvt_usr("MES");
                trans_prd_sap.setEvt_cate(EVT_MES_SAP_PQC);
                trans_prd_sap.setEvt_timestamp(current_timestamp);;
            }else {
                trans_prd_sap  =  new Trans_prd_sap();
                trans_prd_sap.setEvt_seq_id(GUIDGenerator16.javaGUID());
                trans_prd_sap.setOpe_id_fk(ope);
                trans_prd_sap.setSap_id("");
                trans_prd_sap.setPrd_qty(0);
                trans_prd_sap.setMes_update_time(current_timestamp);
                trans_prd_sap.setEvt_usr("MES");
                trans_prd_sap.setEvt_cate(EVT_MES_SAP_PQC);
                trans_prd_sap.setEvt_timestamp(current_timestamp);
            }
            newTranList.add(trans_prd_sap);
        }
        if(newTranList!=null && newTranList.size()>0){
            transPrdSapDao.saveOrUpdateAll(newTranList);
        }
        try {
            if(fpqcrptList.size()>0){
//                baseConnFactory.addSAPREQProc("EPQCRPT", JacksonUtil.toJSONStr(fpqcrptList));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
